package org.example.mvc_start;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.example.mvc_start.demo.Person;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;


@RequestMapping("/hello")
@RestController
public class HelloController {
    @RequestMapping("sayhi")
    public String SayHello(String name) {
        return "Hello " + name;
    }
//    使⽤基本类型来接收参数时, 参数必须传(除boolean类型), 否则会报500错误
//    类型不匹配时, 会报400错误.
    @RequestMapping("/hiageint")
    public String Hiageint(String name,int age) {
        return "Hiage: " + name + ", age: " + age;
    }
    @RequestMapping("/hiage")
    public String Hiage(String name,Integer age) {
        return "Hiage: " + name + ", age: " + age;
    }
    @RequestMapping("/hiperson")
    public String Hiperson(Person person){
        return "Hiperson: " + person.toString();
    }
    @RequestMapping("/hitime")
    public String hitime(@RequestParam(value = "time",required = false) String nowtime){
        return "nowtime "+nowtime;
    }
    @RequestMapping("/hiarry")
    public String hiarry(String[] arry){
        return Arrays.toString(arry);
    }
    @RequestMapping("/hilist")
    public String hilist(@RequestParam List<String> list ){
        return list.toString();
    }
    @RequestMapping("/hijson")
    public String hijson(@RequestBody Person person){
        return person.toString();
    }
    @RequestMapping("/hipath/{id}/{name}")
    public String hipath(@PathVariable int id, @PathVariable("name") String nowname){
        return "hipath: " + id + ", " + nowname;
    }
    @RequestMapping("/hifile")
    public String hifile(@RequestParam("file") MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        file.transferTo(new File("C:\\Users\\22652\\OneDrive\\"+fileName));
        return fileName;
    }
    @RequestMapping("/getCookie")
    public String getCookie(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        if (cookies!=null){
            for (Cookie cookie : cookies){
                System.out.println(cookie.getName() + ":  " + cookie.getValue());
            }
        }
        return "false";
    }
    @RequestMapping("/getCookie1")
    public String getCookie1(@CookieValue("bite" ) String bite ,@CookieValue("hi" ) String hi){
        return bite+ " is "+hi;
    }
    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request){
        HttpSession session = request.getSession();
        if (session!=null){
            session.setAttribute("name","java");
        }
        return "true";
    }
    @RequestMapping("/getSession1")
    public String getSession1(HttpServletRequest request){
        HttpSession session = request.getSession();
        if (session!=null&&session.getAttribute("name")!=null){
            return "name: "+session.getAttribute("name");
        }
        return null;
    }
    @RequestMapping("/getSession2")
    public String getSession2(@SessionAttribute(value = "name",required = false) String nowname){
        return "nowname "+ nowname;
    }
    @RequestMapping("/getSession3")
    public String getSession3(HttpSession session){
        String s= (String) session.getAttribute("name");
        return "s  "+ s;
    }
    @RequestMapping("/getHeader1")
    public String get(HttpServletRequest request){
        String string = request.getHeader("User-Agent");
        return "userAgent1:"+string;
    }
    @RequestMapping("/getHeader2")
    public String get2(@RequestHeader("User-Agent" ) String nowname){
        return "userAgent2:"+nowname;
    }
}
